<?php

namespace common\models;

use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;

/**
 * User model
 *
 * @property string $uuid 主键
 * @property string $username 用户名
 * @property string $password 密码
 * @property string|null $email 电子邮箱
 * @property int $user_type 用户类型:1 教育部;2 省级;3 学校;4 省级专家
 * @property string|null $department_id 所属部门
 * @property int|null $is_login 是否可以登录:0 不能登录;1 可以登录
 * @property string|null $last_visit_time 最后访问时间
 * @property string|null $create_at 创建时间
 * @property string|null $loginip 最后登录ip
 * @property int|null $expire_time 登录失效时间
 * @property int|null $is_online 是否在线:0 否;1 是
 */
class User extends ActiveRecord implements IdentityInterface
{

    private $auth_key;

    public static function tableName()
    {
        return 'sys_users';
    }

    /*public function behaviors()
    {
      return [
        TimestampBehavior::className(),
      ];
    }*/


    public function rules()
    {
        return [
            [['uuid', 'username', 'password', 'user_type'], 'required'],
            [['user_type', 'is_login', 'expire_time', 'is_online'], 'integer'],
            [['last_visit_time', 'create_at'], 'safe'],
            [['uuid', 'department_id'], 'string', 'max' => 36],
            [['username'], 'string', 'max' => 40],
            [['password'], 'string', 'max' => 128],
            [['email'], 'string', 'max' => 50],
            [['loginip'], 'string', 'max' => 20],
            [['username'], 'unique'],
            [['uuid'], 'unique'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'uuid' => 'Uuid',
            'username' => 'Username',
            'password' => 'Password',
            'email' => 'Email',
            'user_type' => 'User Type',
            'department_id' => 'Department ID',
            'is_login' => 'Is Login',
            'last_visit_time' => 'Last Visit Time',
            'create_at' => 'Create At',
            'loginip' => 'Loginip',
            'expire_time' => 'Expire Time',
            'is_online' => 'Is Online',
        ];
    }


    public function validatePassword($password)
    {
        //return Yii::$app->security->validatePassword($password, $this->password_hash);
        return md5($password) == $this->password;
    }


    const USER_TYPE_ADMIN = 100;

    public static function get_user_type()
    {
        return Yii::$app->user->identity->user_type;
    }

    /**
     * @desc
     * @return string[]
     * @Date: 2024/10/31
     */
    public static function login_to_url()
    {
        $user_type = self::get_user_type();
        if ($user_type === self::USER_TYPE_ADMIN) {
            return ['/site/home'];
        }

        return ['site/logout'];
    }


    public static function findIdentity($id)
    {
        return static::findOne(['uuid' => $id]);
    }


    public static function findIdentityByAccessToken($token, $type = null)
    {
        //throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
    }

    public function getId()
    {
        return $this->getPrimaryKey();
    }


    public function getAuthKey()
    {
        //return $this->auth_key;
    }

    public function generateAuthKey()
    {
        //$this->auth_key = Yii::$app->security->generateRandomString();
    }

    public function validateAuthKey($authKey)
    {
        //return $this->getAuthKey() === $authKey;
    }
}
